<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="serarch">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="systemdictionarytypes" @row-dblclick="handledblclick" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" label="编号" width="60">
			</el-table-column>
			<el-table-column prop="name" label="数据字典名"  sortable>
			</el-table-column>
			<el-table-column prop="sn" label="数据字典编号"  sortable>
			</el-table-column>
			<el-table-column label="操作" >
				<template scope="scope">
					<el-button type="info" size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

    <!-- 抽屉页面 -->
    <el-drawer
        title="数据明细表"
        :visible.sync="table"
        direction="rtl"
        size="50%">
      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;" align="right">
            <el-button type="primary" @click="drawerAdd" >新增</el-button>
      </el-col>
        <el-table :data="gridData">
        <el-table-column type="index" label="编号">
        </el-table-column>
        <el-table-column property="name" label="明细名" ></el-table-column>
        <el-table-column type="button" label="操作" >
          <template scope="scope">
            <el-button type="info" size="small" @click="drawerEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="drawerDel(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-col :span="25" class="toolbar">
        <el-pagination layout="prev, pager, next" @current-change="" :page-size="pageSize" :total="total" :current-page="currentPage" style="float:right;">
        </el-pagination>
      </el-col>
    </el-drawer>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" :current-page="currentPage" style="float:right;">
      </el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="字典名" prop="name">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="编号" prop="sn">
					<el-input v-model="editForm.sn" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>


    <!--编辑界面-抽屉-->
    <el-dialog :title="title" :visible.sync="drawerEditFormVisible" :close-on-click-modal="false">
      <el-form :model="drawerForm" label-width="80px" :rules="drawerFormRules" ref="drawerForm">
        <el-form-item label="明细名" prop="name">
          <el-input v-model="drawerForm.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="drawerEditFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="drawerSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>
	</section>
</template>

<script>

	export default {
		data() {
			return {
			  typeid:null,
        table: false,
        row: null,
        title:'',
				filters: {
					name: ''
				},
				systemdictionarytypes: [],
        gridData:[],
				total: 0,
        pageSize: 2,
				currentPage: 1,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
        drawerEditFormVisible : false,
				editLoading: false,
        drawerFormRules: {},
				editFormRules: {
          name: [
						{ required: true, message: '请输入字典名', trigger: 'blur' }
					],
				},
				//编辑界面数据
				editForm: {
					id: null,
					name: '',
          sn: '',
				},

        //编辑界面数据
        drawerForm: {
          id: null,
          name: '',
          types_id: '',
        },
			}
		},
		methods: {
		  // 双击弹出表格 - 事件
      handledblclick(row) {
        this.table = true;
        this.row = row;
        this.typeid = row.id;
        this.getGridData(this.typeid);
      },
      getGridData(typeid) {
        this.$http.get("/sdd/t/"+ typeid).then(result=>{
          this.gridData = result.data;
        })
      },
      //
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getSystemdictionarytypes();

			},
      // // 抽屉-分页
      // handleCurrentChangedrawer(val1) {
      //   this.currentPage = val1;
      //   this.getSystemdictionarydetails();
      // },

      // 高级查询
      serarch(){
        this.currentPage = 1;
        this.getSystemdictionarytypes();
      },
			//获取数据字典
			getSystemdictionarytypes() {
				let para = {
          currentPage: this.currentPage,
          pageSize:this.pageSize,
          keyword: this.filters.keyword
				};
				this.listLoading = true;
        this.$http.post("/sdt",para).then(result=>{
          //NProgress.start();
          this.listLoading = false;
          this.systemdictionarytypes = result.data.rows;
          this.total = result.data.total;
        }).catch(result=>{

        })
			},

      //抽屉删除
      drawerDel: function (index, row) {
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          //NProgress.start();
          console.log(row);
          this.$http.delete("/sdd/"+row.id).then(result=>{
            result = result.data;
            if(result.success){
              this.$message({
                message: result.message,
                type: 'success'
              });
            } else {
              this.$message({
                message: result.message,
                type: 'error'
              });
            }
            this.listLoading = false;
            // 回到首页
            this.currentPage = 1;
            // 加载列表信息
            this.typeid = row.types_id,
            this.getGridData(this.typeid);
          }).catch(result=>{
            this.listLoading = false;
            this.$message({
              message: "系统繁忙，请稍后重试！！！",
              type: 'error'
            });
          })
        }).catch(() => {

        });
      },
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					this.$http.delete("/sdt/"+row.id).then(result=>{
					  result = result.data;
					  if(result.success){
              this.$message({
                message: result.message,
                type: 'success'
              });
            } else {
              this.$message({
                message: result.message,
                type: 'error'
              });
            }
					  // 回到首页
            this.currentPage = 1;
            // 加载列表信息
            this.getSystemdictionarytypes();
          }).catch(result=>{
            this.listLoading = false;
            this.$message({
              message: "系统繁忙，请稍后重试！！！",
              type: 'error'
            });
          })
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
			  this.title = "编辑";
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
        this.title = "新增";
				this.editFormVisible = true;
				this.editForm = {
          id: null,
          name: '',
          sn: '',
				};
			},
      /* 抽屉 */
      //显示编辑界面
      drawerEdit: function (index, row) {
        this.title = "编辑";
        this.drawerEditFormVisible = true;
        this.drawerForm = Object.assign({}, row);
      },
      //显示新增界面
      drawerAdd: function () {
        this.title = "新增";
        this.drawerEditFormVisible = true;
        this.drawerForm = {
          id: null,
          name: '',
          types_id: '',
        };
      },
      //编辑
      drawerSubmit: function () {
        this.$refs.drawerForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.editLoading = true; // 开启按钮加载忙等
              //NProgress.start();
              let para = Object.assign({}, this.drawerForm);
              para.type= {id:this.typeid};
              this.$http.put("/sdd",para).then(result=>{
                this.drawerEditFormVisible = false;
                this.editLoading = false;
                result = result.data;
                if(result.success){
                  this.$message({
                    message: result.message,
                    type: 'success'
                  });
                } else {
                  this.$message({
                    message: result.message,
                    type: 'error'
                  });
                }
                // 回到首页
                this.currentPage = 1;
                // 加载列表信息
                this.getGridData(this.typeid);
              }).catch(result=> {
                this.listLoading = false;
                this.$message({
                  message: "系统繁忙，请稍后重试！！！",
                  type: 'error'
                });
              })
            });
          }
        });
      },
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true; // 开启按钮加载忙等
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
							this.$http.put("/sdt",para).then(result=>{
                this.editFormVisible = false;
                this.editLoading = false;
                result = result.data;
                if(result.success){
                  this.$message({
                    message: result.message,
                    type: 'success'
                  });
                } else {
                  this.$message({
                    message: result.message,
                    type: 'error'
                  });
                }
                // 回到首页
                this.currentPage = 1;
                // 加载列表信息
                this.getSystemdictionarytypes();
              }).catch(result=> {
                this.listLoading = false;
                this.$message({
                  message: "系统繁忙，请稍后重试！！！",
                  type: 'error'
                });
              })
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
          this.$http.patch("/sdt",ids).then(result=>{
            result = result.data;
            if(result.success){
              this.$message({
                message: result.message,
                type: 'success'
              });
            } else {
              this.$message({
                message: result.message,
                type: 'error'
              });
            }
            // 回到首页
            this.currentPage = 1;
            // 加载列表信息
            this.getSystemdictionarytypes();
          }).catch(result=>{
            this.listLoading = false;
            this.$message({
              message: "系统繁忙，请稍后重试！！！",
              type: 'error'
            });
          })
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getSystemdictionarytypes();
		}
	}
</script>

<style scoped>
</style>